/*Akshit Gupta Roll No-2010012
Data Structures and Algorithm Homework 1
To implement the list in python in the c using the b)LINKED LIST
*/

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>	

   struct node
 {                                          /*Declaration of the  struct node*/
   int data;
   struct node *next;
 }*p,*k;

/****************************************************************/
void add( int num )
   {
	struct node *temp;
        temp=malloc(sizeof(struct node));        
        temp->data=num;

       if ( p== NULL)
       {
          p=temp;
          p->next=NULL;
       }
       else
      {
          temp->next=p;
          p=temp;
         }}
/*************************************************************/   
void add1( int num )
   {
	struct node *temp;
        temp=malloc(sizeof(struct node));
        temp->data=num;
       if ( k== NULL)
       {
          k=temp;
          k->next=NULL;
       }
       else
      {
          temp->next=k;
          k=temp;
      }}
/************************************************/
void show(struct node *r)
  {  r=p;
  if(r==NULL)
     {
       printf("NO ELEMENT IN THE LIST :");
       return;
     }
       while(r!=NULL)
    {
      printf(" -> %d ",r->data);
      r=r->next;
    }
     printf("\n");
  }
/***********************************************************************/
void app(int numa)
{
      struct node *q,*t;
      t=malloc(sizeof(struct node));
      t->data = numa;
      t->next = NULL;
   if( p == NULL )
   {
      p=malloc(sizeof(struct node)); 
      p->data = numa;
      p->next = NULL;
   }
   else
   {
        q = p;
      while( q->next!= NULL )
          { q = q->next;
          }
      
      q->next = t;
   }}
/****************************************************************/ 
void ext(struct node*r)
{     r=k;
      struct node* v;
   if( p == NULL )
   {
       p=r;
        }
   else
   {    v=p;
        
      while( v->next!= NULL )
          { v = v->next;
          }
      
      v->next = r;
   }}
/***********************************************************/
struct node *remo(struct node *l, int n)
{
  struct node *co, *po;
  int flae=0;
  for (co = l, po = NULL;
       co != NULL && co->data != n;
       po= co, co = co->next)
       {flae++;
       }
  if(flae==0)
   {printf("\nERROR:Element not found\n\n");
} 
if (co == NULL) 
    {
          return l;             /* n was not found */
  }
  if (po == NULL)
   { 
   l = l->next;       /* n is in the first node */
  }
  else
 {   po->next = co->next;  /* n is in some other node */
  }
  free(co);
  return l;
}
/***********************************************************/
struct node*ins(struct node *ii , int posi,int inso)
{
  struct node *pi,*ci,*ti;
  ti=malloc(sizeof(struct node));
  int ei=0;
  pi=NULL;
  ci=ii;
  ti->data=inso;
   while(ei!=posi & ci!=NULL)
  {
  pi=ci;
  ci=ci->next;
  ++ei;
}
  pi->next=ti;
  ti->next=ci;
  return ii;
}                                                
/****************************************************************/
struct node *pop(struct node *lp, int er)
{
  struct node *cop, *pop;
  int flap=0;
  cop = lp, pop = NULL;
  while(flap!=er &cop != NULL )
       {
        pop= cop; 
        cop = cop->next;
       ++flap;
       }
  if (cop == NULL) 
    {
          return lp;             /* n was not found */
  }
  if (pop == NULL)
   { 
   lp = lp->next;       /* n is in the first node */
  }
  else
 {   pop->next = cop->next;  /* n is in some other node */
  }
  free(cop);
  return lp;
}
/********************************************************************/
void con(struct node *j , int cx)
{struct node*u;
int lc=0;
    for(u=j;u!=NULL;u=u->next)
    {if((u->data)==cx)
    {lc++;
}}
printf("The count is  %d \n ",lc);
}
/***********************************************************************/
void ind(struct node *we , int qcx)
{struct node*ui;
int qrt=0,pe=-1;
   for(ui=we;ui!=NULL;ui=ui->next)            /*It will start from pos= 0*/
    {pe++;
    if((ui->data)==qcx)
    {qrt++;
    break;
}}
if(qrt==0)
{
         printf("Value is  not present\n\n");
         }
else
{
    printf("Value is present at %d\n\n",pe);
}} 
/*****************************************************************************/
void revi(struct node *hd)
{
    struct node *pre = NULL;                                 
   while (hd != NULL)                                
     {     struct node *nt = hd->next;
  hd->next = pre;

        pre = hd;
        hd = nt;
    }    p=pre;
}
/**************************************************************/ 
void ssort(struct node *pa)
{
  int i,j,m,n=0,hold;
 struct node *qs, *ps, *t;
  for( qs = pa ; qs!=NULL ; qs=qs->next)   /*Counting the length of the linked list*/
  {  ++n;
}
  for(i=1 , t = pa  ; i<=n-1 ;  t = t->next , ++i)
    for( j=0 , ps = pa ; j<n-i ;  ps = ps->next , ++j)
       if(ps->data > (ps->next)->data)
       {
	 hold = ps->data;
         ps->data = (ps->next)->data;
         (ps->next)->data = hold;
       }
}
  /*****************************************************************/
/*Start of the  main function */
/******************************************************************/                              
int main()
 {
        int i;
   p=NULL;
  while(1)  
{
printf("\n\n********FUNCTIONS CHART*********\n");
printf("1.To insert the elements in linked list\n");
printf("2.To show\n");
printf("3.append() \n");
printf("4.extend() \n");
printf("5.insert() \n");
printf("6.remove() \n");
printf("7.pop() \n");
printf("8.index() \n");
printf("9.count() \n");
printf("10.sort() \n");
printf("11.reverse() \n");
printf("12.Exit \n");
printf("\nEnter your choice\n");
    scanf(" %d",&i);
     switch(i)
    {
case 1:
     { int num;
        char e;
        printf("Insert the number");
        scanf(" %d",&num);
        add(num);
        printf("Do you wish to add more(y/n)");
        scanf("%s",&e);
        while(e=='Y'|e=='y')
        {
                            printf("Insert the number");
                            scanf(" %d",&num);
                            add(num);
                             printf("Do you wish to add more(y/n)");
                              scanf("%s",&e);
                              }
        break;
        }
case 2:
      {
      printf(" the elements in the list are :");
         show(p);
                  break;
      }
case 3:
      {        
          int aw;
          printf("Enter the element to be appended\n");
          scanf("%d",&aw);
          app(aw);    
          break; 
           }
case 4:
     {
          printf("Enter elements of the list to be extended\n");
        int nume;
        char ex;
        printf("Insert the number");
        scanf(" %d",&nume);
        add1(nume);
        printf("Do you wish to add more(y/n)");
        scanf("%s",&ex);
        while(ex=='Y'|ex=='y')
        {
                            printf("Insert the number");
                            scanf(" %d",&nume);
                            add1(nume);
                             printf("Do you wish to add more(y/n)");
                              scanf("%s",&ex);
        }
        ext(k);
        break;
        }
case 5:
{                  int poi,ioi;
      printf("Enter the element to be inserted\n");
      scanf("%d",&ioi);
      printf("Enter the postion where element is to be inserted\n");
      scanf("%d",&poi);
      ins(p,ioi,poi);
      break;
      }                   
case 6:
      {int ro;
      printf("Enter the element to be removed\n");
      scanf("%d",&ro);
      remo(p,ro);
      break;
      }
case 7:
      {int rop;
      printf("Enter the positon of element to be poped\n");
      scanf("%d",&rop);
      pop(p,rop);
      break;
      } 
case 8 :
     {int wer;
     printf("Enter the value to be indexed\n\n");
     scanf("%d",&wer);
     ind(p,wer);
            break;
            }      
case 9:
         {
               int we,jk;
               printf("Enter the element to be counted\n");
               scanf("%d",&we);
               con(p,we);
               break;
               }   
case 10:
	{
      ssort(p);
      show(p);
        break;
		}
case 11:
	{
      revi(p);
      show(p);
        break;
		}
case 12:
 { 
     printf("\n\nBye Bye Have a nice day\n\n");
  exit(0);
 }
default:{ printf("\n\nWrong Choice entered\n\n");
         break;
}}
    /* end if switch */
}
return 0;
}/* end of main */
